package cn.richx.pushmessage.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * A permission used by Spring Security.
 */
@Entity
@Table(name = "kf_permission")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Permission implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 权限编码
     */
    @NotNull
    @Size(max = 50)
    @Id
    @Column(name = "permission_code", length = 50)
    private String permissionCode;
    /**
     * 权限描述
     */
    @NotNull
    @Size(max = 50)
    @Column(name = "permission_name",length = 50)
    private String permissionName;
    /**
     * 权限类型
     */
    @NotNull
    @Size(max = 10)
    @Column(name = "type",length = 10)
    private String type;
    /**
     * 页面权限名称
     */
    @Size(max = 50)
    @Column(name = "parent_code",length = 50)
    private  String parentCode;

    @OneToMany(mappedBy="permission",fetch=FetchType.EAGER)
    @JsonIgnore
    private Set<UserPermission> userPermissions = new HashSet<>();

    public String getPermissionCode() {
        return permissionCode;
    }

    public void setPermissionCode(String permissionCode) {
        this.permissionCode = permissionCode;
    }

    public String getPermissionName() {
        return permissionName;
    }

    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getParentCode() {
        return parentCode;
    }

    public void setParentCode(String parentCode) {
        this.parentCode = parentCode;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Permission permission = (Permission) o;

        return !(permissionCode != null ? !permissionCode.equals(permission.permissionCode) : permission.permissionCode != null);
    }

    @Override
    public int hashCode() {
        return permissionCode != null ? permissionCode.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "Permission{" +
            "permissionCode='" + permissionCode + '\'' +
            ", permissionName='" + permissionName + '\'' +
            ", type='" + type + '\'' +
            ", parentCode='" + parentCode + '\'' +
            '}';
    }
}
